package whf.framework.tools.generator.db;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import whf.framework.resource.db.ConnectionManager;
import whf.framework.tools.generator.ObjectRelationMapping;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 *
 */
public class Table{
	/**
	 * 表前缀
	 */
	private String tablePre;
	private String tableSchema;
	private String tableName;
	private String tableComment;
	private List<Column> columns = new ArrayList<Column>();
	
	private ConnectionManager connectionManager;
	
	public Table(ConnectionManager connectionManager, String tableSchema, String name, String tablePre, String tableComment){
		this.tableSchema = tableSchema;
		this.tableName = name;
		this.tablePre = tablePre;
		this.connectionManager = connectionManager;
	}
	
	public Connection getConnection(){
		return this.connectionManager.getPooledConnection();
	}
	
	public void addColumn(Column col){
		this.columns.add(col);
	}
	
	public Column findColumnByName(String name){
		for(int i=0;i<this.columns.size();i++){
			Column col = (Column)columns.get(i);
			if(StringUtils.equalsIgnoreCase(col.getColumnName(), name)){
				return col;
			}
		}
		return null;
	}
	
	public Column findPrimaryKeyColumn(){
		for(int i=0;i<this.columns.size();i++){
			Column col = (Column)columns.get(i);
			if(col.isPrimaryKey() && !col.isForeign()){
				return col;
			}
		}
		return null;
	}

	public List<Column> getColumns() {
		return columns;
	}

	public void setColumns(List<Column> columns) {
		this.columns = columns;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String name) {
		this.tableName = name;
	}

	public String getTableSchema() {
		return tableSchema;
	}

	public void setTableSchema(String tableSchema) {
		this.tableSchema = tableSchema;
	}

	public String getTablePre() {
		return tablePre;
	}

	public void setTablePre(String tablePre) {
		this.tablePre = tablePre;
	}
	
	public ObjectRelationMapping toORM(){
		String[] fields = new String[this.columns.size()];
		String[] properties = new String[this.columns.size()];
		String[] types = new String[this.columns.size()];
		for(int i=0;i<this.columns.size();i++){
			Column col = (Column)this.columns.get(i);
			fields[i] = col.getColumnName();
			properties[i]=col.getPropertyName();
			types[i] = col.getPropertyType();
		}
		return new ObjectRelationMapping(this.getBoClassName(), this.tableName, fields, properties, types);
	}
	
	public String getBoClassName(){
		return Utils.getBoClassName(this.tableName, this.tablePre);
	}
	
	public String toString(){
		return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
	}

	/**
	 * @return Returns the tableComment.
	 */
	public String getTableComment() {
		return tableComment;
	}

	/**
	 * @param tableComment The tableComment to set.
	 */
	public void setTableComment(String tableComment) {
		this.tableComment = tableComment;
	}
}